<?php
class TBB_Model_Role extends Zend_Db_Table_Abstract
{	
	protected $_name = 'role';
	protected $_primary = 'role_id';
	

	/**
	 * Get customer role ID
	 * @return array
	 */
	public function getCustomerRoleID()
	{		
		$select = $this->select()->where('role_name = ?', 'customer');
		$r = $this->fetchRow($select);
		return empty($r) ? null : $r->role_id;
	}

	/**
	 * Create role
	 * @param string $roleName
	 * @param string $description
	 * @return int
	 */
	public function createRole($roleName, $description)
	{
		$roleRow = $this->createRow();
		if($roleRow) {
			$roleRow->role_name = $roleName;
			$roleRow->description = $description;
			return $roleRow->save();
		} else {
			throw new Exception('Could not create role. Role not found.');	
		}		
	}
	
	/**
	 * Get role array
	 * @return array
	 */
	public function getRoleArray()
	{
		$select = $this->select();
		$select->order(array('role_name'));
		return $this->fetchAll($select)->toArray();
	}
	
	/**
	 * Update role
	 * @param int $id
	 * @param string $roleName
	 * @param string $description
	 * @return int
	 */
	public function updateRole($id, $roleName, $description)
	{
		$roleRow = $this->find($id)->current();
		if($roleRow) {
			$roleRow->role_name = $roleName;
			$roleRow->description = $description;
			return $roleRow->save();			
		} else {
			throw new Exception('Could not update role. Role not found.');
		}
	}
	
	/**
	 * Delete role
	 * @param int $id
	 * @return int
	 */
	public function deleteRole($id)
	{
		$roleRow = $this->find($id)->current();
		if($roleRow) {
			return $roleRow->delete();
		} else {
			throw new Exception('Could not delete role. Role not found.');
		}
	}
	
}